package hotciv.variant;

import hotciv.framework.*;
import hotciv.standard.*;

import java.util.ArrayList;
import java.util.List;

class GammaCivWorldStrategy implements WorldSetupStrategy {

	public List<Tile> setupWorld(GameTesting g,List<Tile> world) {
		world = new ArrayList<Tile>();
		return defineWorld((int)(Math.round(Math.random())),world);
	}
	
	/* This code fragment is a 'fake-it' that can
	   generate "random" world maps that will work
	   in the skeleton AlphaGame implemementation.
	   Note that this code fragment introduces
	   variability between two different world
	   layouts using a parametric proposal:
	   switching on an integer 'worldType'
	   parameter. GammaCiv must handle variability
	   using a compositional approach instead! */

	  /** define the world.
	   * @param worldType 1 gives one layout while all other
	   * values provide a second world layout.
	   */
	  private List<Tile> defineWorld(int worldType, List<Tile> world) {
	    world = new ArrayList<Tile>();
	    String[] layout;
	    if ( worldType == 1 ) {
	      layout = new String[] {
	          "...ooMooooo.....",
	          "..ohhoooofffoo..",
	          ".oooooMooo...oo.",
	          ".ooMMMoooo..oooo",
	          "...ofooohhoooo..",
	          ".ofoofooooohhoo.",
	          "...ooo..........",
	          ".ooooo.ooohooM..",
	          ".ooooo.oohooof..",
	          "offfoooo.offoooo",
	          "oooooooo...ooooo",
	          ".ooMMMoooo......",
	          "..ooooooffoooo..",
	          "....ooooooooo...",
	          "..ooohhoo.......",
	          ".....ooooooooo..",
	         };
	    } else {
	      layout = new String[] {
	          "...ooo..........",
	          ".ooooo.ooohooM..",
	          ".ooooo.oohooof..",
	          "offfoooo.offoooo",
	          "oooooooo...ooooo",
	          ".ooMMMoooo......",
	          ".....ooooooooo..",
	          "...ooMooooo.....",
	          "..ohhoooofffoo..",
	          "...ofooohhoooo..",
	          ".oooooMooo...oo.",
	          ".ooMMMoooo..oooo",
	          ".ofoofooooohhoo.",
	          "..ooooooffoooo..",
	          "....ooooooooo...",
	          "..ooohhoo.......",
	         };      
	    }
	    String line;
	    for ( int r = 0; r < Game.WORLDSIZE; r++ ) {
	      line = layout[r];
	      for ( int c = 0; c < Game.WORLDSIZE; c++ ) {
	        char tileChar = line.charAt(c);
	        String type = "error";
	        if ( tileChar == '.' ) { type = GameConstants.OCEANS; }
	        if ( tileChar == 'o' ) { type = GameConstants.PLAINS; }
	        if ( tileChar == 'M' ) { type = GameConstants.MOUNTAINS; }
	        if ( tileChar == 'f' ) { type = GameConstants.FOREST; }
	        if ( tileChar == 'h' ) { type = GameConstants.HILLS; }
	        world.add( new StandardTile(type, r, c ));
	        
	        
	      }
	    }
	    
		/** Place units/cities on tiles in world */
		/**
		 * (1,1) red city
		 * (2,0) red archer
		 * (4,1) blue city
		 * (3,2) blue legion
		 */
	    
	    if ( worldType == 1 ) {	    
	    	world.get(1 * Game.WORLDSIZE + 2).addCity(
	    			new StandardCity(StandardPlayer.RED,1));
	    	world.get(13 * Game.WORLDSIZE + 12).addCity(
	    			new StandardCity(StandardPlayer.BLUE,1));
	    	world.get(2 * Game.WORLDSIZE + 3).addUnit(
	    			new StandardUnit(GameConstants.ARCHER,StandardPlayer.RED));
	    	world.get(12 * Game.WORLDSIZE + 10).addUnit(
	    			new StandardUnit(GameConstants.LEGION,StandardPlayer.BLUE));
	    }else{
	    	world.get(1 * Game.WORLDSIZE + 1).addCity(
	    			new StandardCity(StandardPlayer.RED,1));
	    	world.get(14 * Game.WORLDSIZE + 12).addCity(
	    			new StandardCity(StandardPlayer.BLUE,1));
	    	world.get(2 * Game.WORLDSIZE + 3).addUnit(
	    			new StandardUnit(GameConstants.ARCHER,StandardPlayer.RED));
	    	world.get(12 * Game.WORLDSIZE + 10).addUnit(
	    			new StandardUnit(GameConstants.LEGION,StandardPlayer.BLUE));	
	    }
	    return world;
	  }

}